package com.glorysoft.wangnianxinpda.adapter;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glorysoft.wangnianxinpda.adapter.viewholder.DataBindingBaseViewHolder;
import com.glorysoft.wangnianxinpda.databinding.AdapterAbnomalCodeBinding;
import com.glorysoft.wangnianxinpda.entity.AbnomalCodeEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zhangjy
 * @Date 2021/7/1 17:58
 * Description :
 */
public class AbnomalCodeAdapter extends BaseQuickAdapter<AbnomalCodeEntity, DataBindingBaseViewHolder> {
    private List<AbnomalCodeEntity> list = new ArrayList<>();
    public AbnomalCodeAdapter(int layoutResId, @Nullable List<AbnomalCodeEntity> data) {
        super(layoutResId, data);
    }

    public void setList(List<AbnomalCodeEntity> list) {
        this.list = list;
    }

    public List<AbnomalCodeEntity> getList() {
        return list;
    }

    @Override
    protected void convert(DataBindingBaseViewHolder helper, AbnomalCodeEntity item) {
        AdapterAbnomalCodeBinding dataBinding = (AdapterAbnomalCodeBinding) helper.getDataBinding();
        dataBinding.setEntity(item);
        dataBinding.executePendingBindings();
        dataBinding.cbCheckBadCode.setOnClickListener(v ->{
            if (dataBinding.cbCheckBadCode.isChecked()) {
                item.setChecked(true);
                list.add(item);
            }else {
                item.setChecked(false);
                list.remove(item);
            }

        });

        dataBinding.cbCheckBadCode.setChecked(item.isChecked());
    }
    public void setAllChecked(boolean isAllChecked){
//        List<AbnomalCodeEntity> data = getData();
        for (AbnomalCodeEntity entity : getData()) {
            entity.setChecked(isAllChecked);
        }

        if (list != null){
            list.clear();
        }
        if (isAllChecked){
            list.addAll(getData());
        }

        notifyDataSetChanged();
    }
}
